package com.lxc.trigger.http;

import com.alibaba.fastjson.JSON;
import com.lxc.domain.operation.model.entity.*;
import com.lxc.domain.operation.service.IOperationService;
import com.lxc.types.common.OperationRequest;
import com.lxc.types.common.OperationResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author lxc18
 * @date 2024/12/27 10:50
 * @description OperationController
 */
@CrossOrigin // 解决跨域问题@CrossOrigin("https://xxxxx")
@RestController
@RequestMapping("/wg/admin/data")

public class OperationController {
    private Logger logger = LoggerFactory.getLogger(OperationController.class);
    @Resource
    IOperationService service;

    //为什么需要传入interfaceId？不是查询所有的吗？是的，如果interfaceId为空那就是所有的
    @GetMapping(value = "queryApplicationInterface", produces = "application/json;charset=utf-8")
    public OperationResult<OPApplicationInterfaceEntity> queryApplicationInterface(@RequestParam String systemId,
                                                                                   @RequestParam String interfaceId,
                                                                                   @RequestParam String page,
                                                                                   @RequestParam String limit) {
        try {
            logger.info("查询应用接口信息开始 systemId：{} interfaceId：{} page：{} limit：{}", systemId, interfaceId, page, limit);
            OperationRequest<OPApplicationInterfaceEntity> req = new OperationRequest<>(page, limit);
            req.setData(new OPApplicationInterfaceEntity(systemId, interfaceId));
            OperationResult<OPApplicationInterfaceEntity> operationResult = service.queryApplicationInterface(req);
            logger.info("查询应用接口信息完成 operationResult：{}", JSON.toJSONString(operationResult));
            return operationResult;
        } catch (Exception e) {
            logger.error("查询应用接口信息异常 systemId：{} interfaceId：{}", systemId, interfaceId, e);
            return new OperationResult<>(0, null);
        }
    }
    @GetMapping(value = "queryApplicationInterfaceMethodList", produces = "application/json;charset=utf-8")
    public OperationResult<OPApplicationInterfaceMethodEntity> queryApplicationInterfaceMethodList(@RequestParam String systemId,
                                                                                                   @RequestParam String interfaceId,
                                                                                                   @RequestParam String page,
                                                                                                   @RequestParam String limit) {
        try {
            logger.info("查询应用接口方法信息开始 systemId：{} interfaceId：{} page：{} limit：{}", systemId, interfaceId, page, limit);
            OperationRequest<OPApplicationInterfaceMethodEntity> req = new OperationRequest<>(page, limit);
            req.setData(new OPApplicationInterfaceMethodEntity(systemId, interfaceId));
            OperationResult<OPApplicationInterfaceMethodEntity> operationResult = service.queryApplicationInterfaceMethod(req);
            logger.info("查询应用接口方法信息完成 operationResult：{}", JSON.toJSONString(operationResult));
            return operationResult;
        } catch (Exception e) {
            logger.error("查询应用接口方法信息异常 systemId：{} interfaceId：{}", systemId, interfaceId, e);
            return new OperationResult<>(0, null);
        }
    }
    @GetMapping(value = "queryGatewayServer", produces = "application/json;charset=utf-8")
    public OperationResult<OPGatewayServerEntity> queryGatewayServer(@RequestParam String groupId,
                                                                     @RequestParam String page,
                                                                     @RequestParam String limit) {
        try {
            logger.info("查询网关服务数据开始 groupId：{} page：{} limit：{}", groupId, page, limit);
            OperationRequest<String> req = new OperationRequest<>(page, limit);
            req.setData(groupId);
            OperationResult<OPGatewayServerEntity> operationResult = service.queryGatewayServer(req);
            logger.info("查询网关服务数据完成 operationResult：{}", JSON.toJSONString(operationResult));
            return operationResult;
        } catch (Exception e) {
            logger.error("查询网关服务数据异常 groupId：{}", groupId, e);
            return new OperationResult<>(0, null);
        }
    }

    @GetMapping(value = "queryGatewayServerDetail", produces = "application/json;charset=utf-8")
    public OperationResult<OPGatewayServerDetailEntity> queryGatewayServerDetail(@RequestParam String groupId,
                                                                                 @RequestParam String gatewayId,
                                                                                 @RequestParam String page,
                                                                                 @RequestParam String limit) {
        try {
            logger.info("查询网关服务详情数据开始 groupId：{} gatewayId：{} page：{} limit：{}", groupId, gatewayId, page, limit);
            OperationRequest<OPGatewayServerDetailEntity> req = new OperationRequest<>(page, limit);
            req.setData(new OPGatewayServerDetailEntity(groupId, gatewayId));
            OperationResult<OPGatewayServerDetailEntity> operationResult = service.queryGatewayServerDetail(req);
            logger.info("查询网关服务详情数据完成 operationResult：{}", JSON.toJSONString(operationResult));
            return operationResult;
        } catch (Exception e) {
            logger.error("查询网关服务详情数据异常 groupId：{}", groupId, e);
            return new OperationResult<>(0, null);
        }
    }

    @GetMapping(value = "queryGatewayDistribution", produces = "application/json;charset=utf-8")
    public OperationResult<OPGatewayDistributionEntity> queryGatewayDistribution(@RequestParam String groupId,
                                                                                 @RequestParam String gatewayId,
                                                                                 @RequestParam String page,
                                                                                 @RequestParam String limit) {
        try {
            logger.info("查询网关分配数据开始 groupId：{} gatewayId：{} page：{} limit：{}", groupId, gatewayId, page, limit);
            OperationRequest<OPGatewayDistributionEntity> req = new OperationRequest<>(page, limit);
            req.setData(new OPGatewayDistributionEntity(groupId, gatewayId));
            OperationResult<OPGatewayDistributionEntity> operationResult = service.queryGatewayDistribution(req);
            logger.info("查询网关分配数据完成 operationResult：{}", JSON.toJSONString(operationResult));
            return operationResult;
        } catch (Exception e) {
            logger.error("查询网关分配数据异常 groupId：{}", groupId, e);
            return new OperationResult<>(0, null);
        }
    }

    @GetMapping(value = "queryApplicationSystem", produces = "application/json;charset=utf-8")
    public OperationResult<OPApplicationSystemEntity> queryApplicationSystem(@RequestParam String systemId,
                                                                           @RequestParam String systemName,
                                                                           @RequestParam String page,
                                                                           @RequestParam String limit) {
        try {
            logger.info("查询应用系统信息开始 systemId：{} systemName：{} page：{} limit：{}", systemId, systemName, page, limit);
            OperationRequest<OPApplicationSystemEntity> req = new OperationRequest<>(page, limit);
            req.setData(new OPApplicationSystemEntity(systemId, systemName));
            OperationResult<OPApplicationSystemEntity> operationResult = service.queryApplicationSystem(req);
            logger.info("查询应用系统信息完成 operationResult：{}", JSON.toJSONString(operationResult));
            return operationResult;
        } catch (Exception e) {
            logger.error("查询应用系统信息异常 systemId：{} systemName：{}", systemId, systemId, e);
            return new OperationResult<>(0, null);
        }
    }


}
